package com.wu.controller;/**
 * @author sun
 * @date 2022/3/21 - 0:18
 */

import com.wu.pojo.Boardcast;
import com.wu.pojo.BoardcastRedirectcontent;
import com.wu.pojo.vo.BoardcastVo;
import com.wu.service.BoardcastRedirectcontentService;
import com.wu.service.BoardcastService;
import com.wu.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: TODO
 * @author
 * @date 2022/3/21 0:18
 * @version 1.0
 */
@RestController
@Api(tags="Boardcast模块")
@CrossOrigin
@RequestMapping("/boardcast")
public class BoardcastController {
    @Autowired
    private BoardcastService boardcastService;
    @Autowired
    private BoardcastRedirectcontentService boardcastRedirectcontentService;

    @PostMapping("/insertBoardcast")
    @ApiOperation(value="增加Boardcast")
    /*@Transactional(rollbackFor = Exception.class)*/
    public R insertBoardcast(Boardcast boardcast){
        try {
           /* boardcast.setId(Integer.parseInt(RandomStringUtils.randomNumeric(10)));*/
            boardcastService.insertSelective(boardcast);
           /* boardcastRedirectcontent.setBoardcastId(boardcast.getId());
            boardcastRedirectcontentService.insertSelective(boardcastRedirectcontent);*/
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }

    @PostMapping("/insertBoardcastVo")
    @ApiOperation(value="增加BoardcastVo")
    /*@Transactional(rollbackFor = Exception.class)*/
    public R insertBoardcastVo(BoardcastVo boardcastVo){
        try {
            if (boardcastVo.getRedirectcontents()!=null){
                 List<BoardcastRedirectcontent> redirectcontentList = boardcastVo.getRedirectcontents();
                redirectcontentList.forEach(e->{
                    boardcastRedirectcontentService.insertSelective(e);
                });
            }
            boardcastService.insertSelective(boardcastVo);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }


    @GetMapping("/deleteBoardcast/{id}")
    @ApiOperation(value="删除Boardcast")
    /*@Transactional(rollbackFor = Exception.class)*/
    public R deleteBoardcast(Integer id){
        try {
            boardcastService.deleteByPrimaryKey(id);
            /*boardcastRedirectcontentService.deleteByBoardcastId(id);*/
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PutMapping("/updateBoardcast")
    @ApiOperation(value="更新Boardcast")
    /*@Transactional(rollbackFor = Exception.class)*/
    public R updateBoardcast(@RequestBody Boardcast boardcast){
        try {
            boardcastService.updateByPrimaryKeySelective(boardcast);
           /* boardcastRedirectcontentService.updateByPrimaryKeySelective(boardcastRedirectcontent);*/
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findBoardcastList")
    @ApiOperation(value="查询BoardcastList")
    public R findBoardcast(Boardcast boardcast){
        try {
            List<BoardcastVo> list= boardcastService.findBoardcastVo(boardcast);
            return R.ok().data("boardcastList",list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findBoardcastById/{id}")
    @ApiOperation(value="通过id查询Boardcast")
    public R findBoardcastById(Integer id){
        try {
            Boardcast boardcast= boardcastService.selectByPrimaryKey(id);
            return R.ok().data("boardcast",boardcast);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

}
